package com.app.teaware.set;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.teaware.R;
import com.app.teaware.bean.BaseListData;
import com.app.teaware.bean.BaseListDataListBean;
import com.app.teaware.goods.GoodsTestActivity;
import com.app.teaware.home.HomeBaseActivity;
import com.app.teaware.login.LoginActivity;
import com.app.teaware.util.UHFUtil;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.ActivityTool;

/**
 * UHF设置
 */
public class UHFSetActivity extends HomeBaseActivity {


    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    BaseListDataListBean mListBean;
    AdapterEnter mAdapter;
    private boolean mModuleAvailable =  false;

    QMUIDialog.MultiCheckableDialogBuilder builder;

    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.base_recycler_with_top);
        recyclerview.setLayoutManager(new GridLayoutManager(mContext, 2));
    }

    @Override
    public void initData(Bundle savedInstanceState) {

        super.initData(savedInstanceState);
        initData();
        setTitle("UHF设置");
        initUHF();
    }

    private void initData() {
        mListBean = new BaseListDataListBean();
        BaseListData data;
        data = new BaseListData("0","模块上电",R.mipmap.icon_uhf_open,R.mipmap.icon_uhf_close);
        mListBean.getList().add(data);
//        data = new BaseListData("1","盘点触发按钮",R.mipmap.icon_uhf_touch_type,R.drawable.base_img_default_square);
//        mListBean.getList().add(data);
        data = new BaseListData("2","盘点测试",R.mipmap.icon_uhf_touch_type,R.drawable.base_img_default_square);
        mListBean.getList().add(data);
        setGridEnterView();
    }

    private void initUHF(){
        hideLoading();
        mModuleAvailable = (UHFUtil.getInstance().getUHFModuleInfo() == null ? false : true);
        if(!mModuleAvailable) showReloadModuleWindow();//显示重新检测的对话框
    }

    //未检测到模块,弹出窗口
    private void showReloadModuleWindow() {
        new QMUIDialog.MessageDialogBuilder(mContext).setTitle(R.string.app_warn)
                .setMessage(R.string.uhf_module_unavailable)
                .addAction(R.string.again, new QMUIDialogAction.ActionListener() {
            @Override
            public void onClick(QMUIDialog dialog, int index) {
                dialog.dismiss();
                showLoading();
                UHFUtil.getInstance().loadUHFModule();
                initUHF();
            }
        }).addAction(0, R.string.app_exit, QMUIDialogAction.ACTION_PROP_NEGATIVE, new QMUIDialogAction.ActionListener() {
            @Override
            public void onClick(QMUIDialog dialog, int index) {
                LoginActivity.toLoginAllClear(mContext);
            }
        }).create().show();

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


    @Override
    protected void onResume() {
        super.onResume();
//        QMUIStatusBarHelper.setStatusBarDarkMode(mContext);
        if(mAdapter != null){
            mAdapter.notifyDataSetChanged();
            recyclerview.setAdapter(mAdapter);
        }
    }


    @Override
    public void onDoubleClick() {
        super.onDoubleClick();
    }



    void setGridEnterView() {
        if(mAdapter == null){
            mAdapter = new AdapterEnter();
            recyclerview.setAdapter(new AdapterEnter());
        }else {
            mAdapter.notifyDataSetChanged();
        }
    }



    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        if (resultCode == Activity.RESULT_OK) {
//            refreshData();
//        }
    }


    public class AdapterEnter extends RecyclerView.Adapter<AdapterEnter.ViewHolder> {

        public AdapterEnter() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public AdapterEnter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.home_enter_grid_item, viewGroup, false);
            return new AdapterEnter.ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(AdapterEnter.ViewHolder viewHolder, int position) {
            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            return mListBean == null ? 0 : mListBean.getList().size();
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.text_name)
            public TextView text_name;
            @BindView(R.id.text_count)
            public TextView text_count;
            @BindView(R.id.img_icon)
            public ImageView img_icon;

            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void bind(int position) {
                BaseListData data = mListBean.getList().get(position);
                text_name.setText(data.getName());
                img_icon.setImageResource(data.getPath());
                list_item.setTag(data);
            }

            @RequiresApi(api = Build.VERSION_CODES.N)
            @OnClick(R.id.list_item)
            public void onItemClick(View v) {
                BaseListData data = (BaseListData) v.getTag();
                switch (data.getId()){
                    case "0":
                        if(UHFUtil.getStatus()){
                            showLoadingDialog("下电中");
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    UHFUtil.close();
                                    dismissProgress();
                                    notifyDataSetChanged();
                                }
                            },1000);

                        }else {
                            showLoadingDialog(getString(R.string.power_oning));
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    UHFUtil.open();
                                    dismissProgress();
                                    notifyDataSetChanged();
                                }
                            },1000);
                        }
                        break;
                    case "1":
//                        if(builder == null){
//                            builder = new QMUIDialog.MultiCheckableDialogBuilder(mContext)
//                                    .setSkinManager(QMUISkinManager.defaultInstance(mContext))
//                                    .addItems(UHFUtil.triggerModeItems, new DialogInterface.OnClickListener() {
//                                        @Override
//                                        public void onClick(DialogInterface dialog, int which) {
//
//                                        }
//                                    });
//                            builder.addAction("取消", new QMUIDialogAction.ActionListener() {
//                                @Override
//                                public void onClick(QMUIDialog dialog, int index) {
//                                    dialog.dismiss();
//                                }
//                            });
//                            builder.addAction("提交", new QMUIDialogAction.ActionListener() {
//                                @Override
//                                public void onClick(QMUIDialog dialog, int index) {
//                                    UHFUtil.setTriggerMode(builder.getCheckedItemIndexes());
//                                    dialog.dismiss();
//                                    showToast("设置成功");
//                                }
//                            });
//                        }
//                        builder.setCheckedItems(UHFUtil.getTriggerMode());
//                        builder.show();
                        break;
                    case "2":
                        ActivityTool.skipActivity(mContext, GoodsTestActivity.class);
                        break;
                }

            }
        }
    }

}
